package com.eway.esb.service.rest;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

import org.apache.cxf.staxutils.DepthXMLStreamReader;
import org.apache.cxf.transport.AbstractConduit;
import org.apache.cxf.transport.Conduit;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.QName;
import org.mule.api.transformer.TransformerException;
import org.mule.module.xml.util.XMLUtils;
import org.mule.transformer.AbstractTransformer;
import org.mule.transport.cxf.transport.MuleUniversalConduit;

public class NfyRequestTransformer extends AbstractTransformer{
	
	public NfyRequestTransformer(){
		super();
        this.registerSourceType(Object.class);
        this.setReturnClass(Map.class);
	}

	@Override
	protected Object doTransform(Object obj, String s)
			throws TransformerException {
		System.out.println("#####################################"+obj);
		System.out.println("#####################################"+obj.getClass().getName());
		/*if (obj instanceof Conduit) { 
			//璇诲彇XMLStreamReader涓殑鍐呭
			MuleUniversalConduit conduit=(MuleUniversalConduit)obj;
		    XMLStreamReader streamReader = (javax.xml.stream.XMLStreamReader) obj; 
		    Document document;
			try {
				document = XMLUtils.toDocument(streamReader);
				System.out.println("娑堟伅娴�1鍐呭锛�"+document.asXML());
				document = XMLUtils.toDocument(streamReader);
				Element rootElement = document.getRootElement();
				Element node = rootElement.element("patientId");
				System.out.println(node.getText());
				
				QName qname=new QName("patientCardNo");
				node.setQName(qname);
				System.out.println("娑堟伅娴�2鍐呭锛�"+document.asXML());
				//鐢熸垚娑堟伅娴佸苟杩斿洖
				XMLInputFactory inputFactory = XMLInputFactory.newInstance(); 
				XMLStreamReader xmlStreamReader;
				xmlStreamReader = inputFactory .createXMLStreamReader(new StringReader(document.asXML()));
				DepthXMLStreamReader reader=new DepthXMLStreamReader(xmlStreamReader);
				return reader;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    
		    
		}*/
		//return obj;
		try{
			Map<String,String> map=new HashMap<String,String>();
			if (obj instanceof javax.xml.stream.XMLStreamReader) { 
				//读取XMLStreamReader中的内容
			    XMLStreamReader streamReader = (javax.xml.stream.XMLStreamReader) obj; 
			    Document document;

				document = XMLUtils.toDocument(streamReader);
				Element rootElement = document.getRootElement();
				Element node = rootElement.element("patientId");
				String patientCardNo=node.getText();
				
				map.put("patientCardNo", patientCardNo);
			    System.out.println("#####################################"+s.getClass());
			    
			}
			return map;
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} 
	}

}
